package com.investigation.activity.thanks;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.lifecycle.AndroidViewModel;

import com.google.android.material.snackbar.Snackbar;
import com.investigation.BuildConfig;
import com.investigation.ExcelBean;
import com.investigation.ExcelUtil;
import com.investigation.TXTManager;
import com.investigation.databinding.ActivityThanksBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ThanksVM extends AndroidViewModel {
    @SuppressLint("StaticFieldLeak")
    private ThanksActivity thanksActivity;
    public ThanksVM(@NonNull Application application) {
        super(application);
    }
    public void setBinding(ActivityThanksBinding binding, ThanksActivity thanksActivity) {
        this.thanksActivity=thanksActivity;
    }
    public void share() {
        TXTManager txtManager=new TXTManager();
        String filePath = txtManager.rootXMLPath +"/config.xls";
        String[] title = {"时间", "第一题", "第二题","第三题", "第四题","第五题", "第六题","第七题", "第八题","第九题", "第十题","第十一题", "第十二题","第十三题", "第十四题","第十五题", "第十六题"};
        String sheetName = "记录";
        List<ExcelBean> excelBeanList = new ArrayList<>();
        try {
            JSONArray array=new JSONArray(txtManager.read("config"));
            for(int i=0;i<array.length();i++) {
                JSONObject job = array.getJSONObject(i);
                @SuppressLint("SimpleDateFormat") SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time=ymd.format(new Date(Long.parseLong(job.getString("time")) * 1000L));
                ExcelBean excelBean = new ExcelBean(time, job.getString("data").split("/"));
                excelBeanList.add(excelBean);
            }
        } catch (JSONException ignored) { }
        ExcelUtil.initExcel(filePath, sheetName, title);
        ExcelUtil.writeObjListToExcel(excelBeanList, filePath, thanksActivity.getApplicationContext());

        File file =new File(txtManager.rootXMLPath +"/config.xls");
        if (file.exists()){
            shareFile(thanksActivity,txtManager.rootXMLPath+"/config.xls");
        }
    }
    public void exit(View view){
        thanksActivity.finish();
    }
    public void shareFile(Activity context, String  fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("application/vnd.ms-excel");//此处可发送多种文件
            share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider",file);
                share.putExtra(Intent.EXTRA_STREAM, contentUri);
                share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            context.startActivity(Intent.createChooser(share, "share to"));
        }else {
            Snackbar.make(thanksActivity.getCurrentFocus(),"文件不存在",Snackbar.LENGTH_SHORT).show();
        }
    }
}
